package com.example.springbootchapter09.controllor;

import com.example.springbootchapter09.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

@RestController
public class AsyncController {
    @Autowired
    private AsyncService asyncService;
    @GetMapping("/noReturnAsync")
    public String noReturnAsync() {
        asyncService.noReturnAsyncTask();
        return "无返回值异步任务已触发";
    }
    @GetMapping("/returnAsync")
    public String returnAsync() {
        try {
            Future<String> result = asyncService.returnAsyncTask();
            while (!result.isDone()) {
                // 等待任务完成
            }
            return result.get();
        } catch (Exception e) {
            e.printStackTrace();
            return "获取异步任务结果出错";
        }
    }
}